package com.ibeeking.found.upms.s.rest.controller.v1;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.upms.s.rest.service.IShopRoleAuthService;
import com.ibeeking.found.upms.service.common.bo.ShopRoleTenantAuthRoleBO;
import com.ibeeking.found.upms.service.common.param.AuthRoleShopRoleParam;
import com.ibeeking.found.upms.service.common.param.ShopRoleAuthRoleParam;
import com.ibeeking.found.upms.service.common.vo.TenantAuthRoleVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName RoleAuthController控制器
 * @Description 租户角色授权管理
 * @Author ibeeking
 * @Date 2020-12-02 15:29
 **/
@RestController
@RequestMapping("/b/roleAuth/v1")
public class ShopRoleAuthController {

    @Resource
    private IShopRoleAuthService shopRoleAuthService;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询租户有效可授权角色列表")
    @GetMapping(value = "/effectiveAuthList")
    public List<TenantAuthRoleVO> effectiveAuthList() {
        return shopRoleAuthService.effectiveAuthList();
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "商户角色关联租户授权角色")
    @PostMapping(value = "/authRoleByShopRoleId")
    public Boolean authRoleByShopRoleId(@LoginUser CurrentUser currentUser, @RequestBody @Validated AuthRoleShopRoleParam param) {
        return shopRoleAuthService.authRoleByShopRoleId(currentUser.getCurrentShop().getShopId(), param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "租户授权角色关联商户角色")
    @PostMapping(value = "/shopRoleByAuthRoleId")
    public Boolean shopRoleByAuthRoleId(@LoginUser CurrentUser currentUser, @RequestBody @Validated ShopRoleAuthRoleParam param) {
        return shopRoleAuthService.shopRoleByAuthRoleId(currentUser.getCurrentShop().getShopId(), param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "根据商户角色查询已授权租户角色id列表")
    @GetMapping(value = "/queryByShopRoleId/{shopRoleId}")
    public List<String> queryByShopRoleId(@LoginUser CurrentUser currentUser, @PathVariable("shopRoleId") @NotNull(message = "商户角色id不能为空") Long shopRoleId) {
        List<ShopRoleTenantAuthRoleBO> shopRoleTenantAuthRoleBos = shopRoleAuthService.queryByShopRoleId(currentUser.getCurrentShop().getShopId(), shopRoleId);
        return shopRoleTenantAuthRoleBos.parallelStream().map(s -> String.valueOf(s.getTenantAuthId())).distinct().collect(Collectors.toList());
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "根据租户授权角色查询已被授权商户角色id列表")
    @GetMapping(value = "/queryByAuthRoleId/{authRoleId}")
    public List<String> queryByAuthRoleId(@LoginUser CurrentUser currentUser, @PathVariable("authRoleId") @NotNull(message = "租户授权角色id不能为空") Long authRoleId) {
        List<ShopRoleTenantAuthRoleBO> shopRoleTenantAuthRoleBos = shopRoleAuthService.queryByAuthRoleId(currentUser.getCurrentShop().getShopId(), authRoleId);
        return shopRoleTenantAuthRoleBos.parallelStream().map(s -> String.valueOf(s.getShopRoleId())).distinct().collect(Collectors.toList());
    }
}
